/**
 * this file is to demonstrator the layout of swing ui
 * the JFrame's default layout manager is BorderLayout
 * the JPanel's default layout manager is FlowLayout
 */
package v1c09;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * @author gjunming@gmail.com
 * @version Aug 10, 2009 1:45:08 PM
 */
public class LayoutTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {

			@Override
			public void run() {
				LayoutFrame layoutFrame = new LayoutFrame();
				layoutFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				layoutFrame.setVisible(true);
			}
		});
	}
}

class LayoutFrame extends JFrame {

	private static final long serialVersionUID = 1L;
	private static final int FWIDTH = 300;
	private static final int FHEIGHT = 200;

	public LayoutFrame() {
		setTitle("Calculateor");
		setSize(FWIDTH, FHEIGHT);
		add(new LayoutPanel());
	}
}

class LayoutPanel extends JPanel {

	private static final long serialVersionUID = 1L;
	private double result;
	private String lastCommand;
	private boolean start;
	private final JButton showing;

	public LayoutPanel() {
		setLayout(new BorderLayout());

		result = 0;
		lastCommand = "=";
		start = true;

		showing = new JButton("0");
		add(showing, BorderLayout.NORTH);

		JPanel doingPanel = new JPanel();
		doingPanel.setLayout(new GridLayout(4, 4));
		add(doingPanel, BorderLayout.CENTER);

		ActionListener insert = new InsertAction();
		ActionListener command = new CommandAction();

		doingPanel.add(makeButton("1", insert));
		doingPanel.add(makeButton("2", insert));
		doingPanel.add(makeButton("3", insert));
		doingPanel.add(makeButton("+", command));

		doingPanel.add(makeButton("4", insert));
		doingPanel.add(makeButton("5", insert));
		doingPanel.add(makeButton("6", insert));
		doingPanel.add(makeButton("-", command));

		doingPanel.add(makeButton("7", insert));
		doingPanel.add(makeButton("8", insert));
		doingPanel.add(makeButton("9", insert));
		doingPanel.add(makeButton("*", command));

		doingPanel.add(makeButton("0", insert));
		doingPanel.add(makeButton(".", insert));
		doingPanel.add(makeButton("=", command));
		doingPanel.add(makeButton("/", command));
	}

	public JButton makeButton(String insertKey, ActionListener listerner) {
		JButton button = new JButton(insertKey);
		button.addActionListener(listerner);
		return button;
	}

	private class InsertAction implements ActionListener {

		/*
		 * @see
		 * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent
		 * * )
		 */
		@Override
		public void actionPerformed(ActionEvent e) {
			String insertKey = e.getActionCommand();
			if (start) {
				showing.setText("");
				start = false;
			}
			showing.setText(showing.getText() + insertKey);
		}

	}

	private class CommandAction implements ActionListener {

		/*
		 * @see
		 * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent
		 * )
		 */
		@Override
		public void actionPerformed(ActionEvent e) {
			String command = e.getActionCommand();
			if (start) {
				// parse the nagitive
				if (command.equals("-")) {
					showing.setText(command);
					start = false;
				} else {
					lastCommand = command;
				}
			} else {
				calculate(Double.parseDouble(showing.getText()));
				lastCommand = command;
				start = true;
			}
		}
	}

	public void calculate(double x) {
		if (lastCommand.equals("=")) {
			result = x;
		} else if (lastCommand.equals("+")) {
			result += x;
		} else if (lastCommand.equals("-")) {
			result -= x;
		} else if (lastCommand.equals("*")) {
			result *= x;
		} else if (lastCommand.equals("/")) {
			result /= x;
		}
		showing.setText("" + result);
	}
}
